# Load packages
library(ggplot2)
library(dplyr)
library(tidyr)
library(tidyverse)
library(plotly)
# Read datasets/confirmed_cases_worldwide.csv into confirmed_cases_worldwide
coronavirus <- read_csv("./data/coronavirus.csv")
Parsed with column specification:
cols(
date = col_date(format = ""),
province = col_logical(),
country = col_character(),
lat = col_double(),
long = col_double(),
type = col_character(),
cases = col_double()
)
45800 parsing failures.
row col expected actual file
37001 province 1/0/T/F/TRUE/FALSE Alberta './data/coronavirus.csv'
37002 province 1/0/T/F/TRUE/FALSE Alberta './data/coronavirus.csv'
37003 province 1/0/T/F/TRUE/FALSE Alberta './data/coronavirus.csv'
37004 province 1/0/T/F/TRUE/FALSE Alberta './data/coronavirus.csv'
37005 province 1/0/T/F/TRUE/FALSE Alberta './data/coronavirus.csv'
..... ........ .................. ....... ........................
See problems(...) for more details.
head(coronavirus)
str(coronavirus)
tibble [157,000 × 7] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
$ date : Date[1:157000], format: "2020-01-22" "2020-01-23" "2020-01-24" ...
$ province: logi [1:157000] NA NA NA NA NA NA ...
$ country : chr [1:157000] "Afghanistan" "Afghanistan" "Afghanistan" "Afghanistan" ...
$ lat : num [1:157000] 33.9 33.9 33.9 33.9 33.9 ...
$ long : num [1:157000] 67.7 67.7 67.7 67.7 67.7 ...
$ type : chr [1:157000] "confirmed" "confirmed" "confirmed" "confirmed" ...
$ cases : num [1:157000] 0 0 0 0 0 0 0 0 0 0 ...
- attr(*, "problems")= tibble [45,800 × 5] (S3: tbl_df/tbl/data.frame)
..$ row : int [1:45800] 37001 37002 37003 37004 37005 37006 37007 37008 37009 37010 ...
..$ col : chr [1:45800] "province" "province" "province" "province" ...
..$ expected: chr [1:45800] "1/0/T/F/TRUE/FALSE" "1/0/T/F/TRUE/FALSE" "1/0/T/F/TRUE/FALSE" "1/0/T/F/TRUE/FALSE" ...
..$ actual : chr [1:45800] "Alberta" "Alberta" "Alberta" "Alberta" ...
..$ file : chr [1:45800] "'./data/coronavirus.csv'" "'./data/coronavirus.csv'" "'./data/coronavirus.csv'" "'./data/coronavirus.csv'" ...
- attr(*, "spec")=
.. cols(
.. date = col_date(format = ""),
.. province = col_logical(),
.. country = col_character(),
.. lat = col_double(),
.. long = col_double(),
.. type = col_character(),
.. cases = col_double()
.. )
# # See the result
bycountry <- coronavirus %>%
filter(type == "confirmed") %>%
group_by(country) %>%
summarise(total_cases = sum(cases)) %>%
arrange(-total_cases)
`summarise()` ungrouping output (override with `.groups` argument)
bycountry
confirmed_cases = coronavirus %>%
group_by(date) %>%
filter(type == "confirmed") %>%
summarize(cases = sum(cases)) %>%
mutate(cumsum = cumsum(cases))
`summarise()` ungrouping output (override with `.groups` argument)
# confirmed_cases_china = coronavirus %>%
# group_by(date) %>%
# filter(country == "China", type == "confirmed")%>%
# summarize(cases = sum(cases)) %>%
# mutate(cumsum = cumsum(cases))
# confirmed_cases_china
(confirmed_cases)
NA
NA
NA
NA
NA
coronavirus %>%
filter(date == max(date)) %>%
select(country, type, cases) %>%
group_by(country, type) %>%
summarise(total_cases = sum(cases)) %>%
pivot_wider(names_from = type,
values_from = total_cases) %>%
arrange(-confirmed)
`summarise()` regrouping output by 'country' (override with `.groups` argument)
coronavirus %>%
group_by(type, date) %>%
summarise(total_cases = sum(cases)) %>%
pivot_wider(names_from = type, values_from = total_cases) %>%
arrange(date) %>%
mutate(active = confirmed - death - recovered) %>%
mutate(active_total = cumsum(active),
recovered_total = cumsum(recovered),
death_total = cumsum(death)) %>%
plot_ly(x = ~ date,
y = ~ active_total,
name = 'Active',
fillcolor = '#1f77b4',
type = 'scatter',
mode = 'none',
stackgroup = 'one') %>%
add_trace(y = ~ death_total,
name = "Death",
fillcolor = '#E41317') %>%
add_trace(y = ~recovered_total,
name = 'Recovered',
fillcolor = 'forestgreen') %>%
layout(title = "Distribution of Covid19 Cases Worldwide",
legend = list(x = 0.1, y = 0.9),
yaxis = list(title = "Number of Cases"),
xaxis = list(title = "Source: Johns Hopkins University Center for Systems Science and Engineering"))
`summarise()` regrouping output by 'type' (override with `.groups` argument)
`arrange_()` is deprecated as of dplyr 0.7.0.
Please use `arrange()` instead.
See vignette('programming') for more help
This warning is displayed once every 8 hours.
Call `lifecycle::last_warnings()` to see where this warning was generated.
conf_df <- coronavirus %>%
filter(type == "confirmed") %>%
group_by(country) %>%
summarise(total_cases = sum(cases)) %>%
arrange(-total_cases) %>%
mutate(parents = "Confirmed") %>%
ungroup()
`summarise()` ungrouping output (override with `.groups` argument)
plot_ly(data = conf_df,
type= "treemap",
values = ~total_cases,
labels= ~ country,
parents= ~parents,
domain = list(column=0),
name = "Confirmed",
textinfo="label+value+percent parent")
ggplot(confirmed_cases, aes(x = date, y = cum_cases)) +
geom_line(aes(x = date, y = cum_cases)) +
ylab("Cumulative confirmed cases")
Error in FUN(X[[i]], ...) : object 'cum_cases' not found
head(coronavirus)
coronavirus = tibble::rowid_to_column(coronavirus, "ID")
head(coronavirus)
# coronavirus = coronavirus %>%
# group_by(ID) %>%
# mutate(cum_cases = cumsum(cases))
# coronavirus %>% head(70)
mutate(group_by(coronavirus, ID), cumsum = cumsum(cases))
# df <- data.frame(id = rep(1:3, each = 5),
# hour = rep(1:5, 3),
# value = sample(1:15))
#
# mutate(group_by(df,id), cumsum=cumsum(value))
confirmed_cases_usa = coronavirus %>%
group_by(date) %>%
filter(country == "US", type == "confirmed")%>%
summarize(cases = sum(cases)) %>%
mutate(cumsum = cumsum(cases))
`summarise()` ungrouping output (override with `.groups` argument)
confirmed_cases_usa
NA
plt_cum_confirmed_cases_usa <- ggplot(confirmed_cases_usa, aes(date, cumsum)) +
geom_line() +
ylab("Cumulative confirmed cases")
# See the plot
plt_cum_confirmed_cases_usa
# Filter for USA, from Feb 15
usa_after_march1 <- confirmed_cases_usa %>%
filter(date >= "2020-03-1")
# Using china_after_feb15, draw a line plot cum_cases vs. date
# Add a smooth trend line using linear regression, no error bars
ggplot(usa_after_march1, aes(date, cumsum)) +
geom_line() +
geom_smooth(method = "lm", formula = 'y ~ x', se = FALSE) +
ylab("Cumulative confirmed cases")
confirmed_cases_china = coronavirus %>%
group_by(date) %>%
filter(country == "China", type == "confirmed")%>%
summarize(cases = sum(cases)) %>%
mutate(cumsum = cumsum(cases))
`summarise()` ungrouping output (override with `.groups` argument)
confirmed_cases_china
confirmed_cases_china %>%
group_by(date) %>%
summarize(cases = sum(cases)) %>%
select(date, country, cases, cumsum)
`summarise()` ungrouping output (override with `.groups` argument)
Error: Can't subset columns that don't exist.
x Column `country` doesn't exist.
plt_cum_confirmed_cases_china <- ggplot(confirmed_cases_china, aes(date, cumsum)) +
geom_line() +
ylab("Cumulative confirmed cases")
# See the plot
plt_cum_confirmed_cases_china
who_events <- tribble(
~ date, ~ event,
"2020-01-30", "Global health emergency declared",
"2020-03-11", "Pandemic declared",
"2020-02-13", "China reporting change"
) %>%
mutate(date = as.Date(date))
# Using who_events, add vertical dashed lines with an xintercept at date
# and text at date, labeled by event, and at 100000 on the y-axis
plt_cum_confirmed_cases_china +
geom_vline(aes(xintercept = date), data = who_events, linetype = "dashed") +
geom_text(aes(x = date, label = event), data = who_events, y = 1e5)
# Filter for China, from Feb 15
china_after_feb15 <- confirmed_cases_china %>%
filter(date >= "2020-02-15")
# Using china_after_feb15, draw a line plot cum_cases vs. date
# Add a smooth trend line using linear regression, no error bars
ggplot(china_after_feb15, aes(date, cumsum)) +
geom_line() +
geom_smooth(method = "lm", formula = 'y ~ x', se = FALSE) +
ylab("Cumulative confirmed cases")
not_china = coronavirus %>%
group_by(date) %>%
filter(country != "China", type == "confirmed")%>%
summarize(cases = sum(cases)) %>%
mutate(cumsum = cumsum(cases))
`summarise()` ungrouping output (override with `.groups` argument)
not_china
# not_china %>%
# group_by(date) %>%
# summarize(cases = sum(cases)) %>%
# select(date, country, cases, cumsum)
glimpse(not_china)
Rows: 200
Columns: 3
$ date <date> 2020-01-22, 2020-01-23, 2020-01-24, 2020-01-25, 2020-01-26, 2020-01-27, 2…
$ cases <dbl> 7, 4, 10, 7, 15, 7, 19, 10, 14, 32, 22, 10, 14, 20, 12, 12, 70, 30, 15, 84…
$ cumsum <dbl> 7, 11, 21, 28, 43, 50, 69, 79, 93, 125, 147, 157, 171, 191, 203, 215, 285,…
not_usa = coronavirus %>%
Warning messages:
1: In readChar(file, size, TRUE) : truncating string with embedded nuls
2: In readChar(file, size, TRUE) : truncating string with embedded nuls
3: In readChar(file, size, TRUE) : truncating string with embedded nuls
4: In readChar(file, size, TRUE) : truncating string with embedded nuls
5: In readChar(file, size, TRUE) : truncating string with embedded nuls
6: In readChar(file, size, TRUE) : truncating string with embedded nuls
7: In readChar(file, size, TRUE) : truncating string with embedded nuls
group_by(date) %>%
filter(country != "US", type == "confirmed")%>%
summarize(cases = sum(cases)) %>%
mutate(cumsum = cumsum(cases))
`summarise()` ungrouping output (override with `.groups` argument)
not_usa
# Using not_usa, draw a line plot cum_cases vs. date
# Add a smooth trend line using linear regression, no error bars
plt_not_usa_trend_lin <- ggplot(not_usa, aes(date, cumsum)) +
geom_line() +
geom_smooth(method = "lm", formula = 'y ~ x', se = FALSE) +
ylab("Cumulative confirmed cases")
# See the result
plt_not_usa_trend_lin
not_china2 = coronavirus %>%
group_by(date) %>%
filter(country != "China", type == "confirmed")%>%
summarize(cases = sum(cases)) %>%
mutate(cumsum = cumsum(cases))
`summarise()` ungrouping output (override with `.groups` argument)
not_china2
world_after_feb15 <- all_countries %>%
filter(date >= "2020-02-15")
Error in eval(lhs, parent, parent) : object 'all_countries' not found
# Using not_china, draw a line plot cum_cases vs. date
# Add a smooth trend line using linear regression, no error bars
plt_not_china_trend_lin <- ggplot(not_china, aes(date, cumsum)) +
geom_line() +
geom_smooth(method = "lm", formula = 'y ~ x', se = FALSE) +
ylab("Cumulative confirmed cases")
# See the result
plt_not_china_trend_lin
plt_not_china_trend_lin +
scale_y_log10()
bycountry <- coronavirus %>%
mutate(date = as.Date(date))%>%
filter(type == "confirmed") %>%
group_by(country, date) %>%
summarise(total_cases = sum(cases)) %>%
arrange(-total_cases)
`summarise()` regrouping output by 'country' (override with `.groups` argument)
Filter by top 7 countries
target = c("Brazil", "India", "Mexico", "Peru", "Russia", "South Africa", "US")
top_7countries = bycountry %>%
filter(country %in% target) %>%
filter(total_cases > 0)
top_7countries
NA
plt_top_7countries = ggplot(top_7countries, aes(date, total_cases)) +
geom_line(aes(group = country, color = country))+
ylab("Cumulative confirmed cases")
plt_top_7countries
plt_top_7countries +
scale_y_log10()
ggplot(top_7countries, aes(x= country, y = total_cases)) +
geom_boxplot(aes(color = country))
income_hist = clean %>% filter(top_7countries, country =='US') %>%
ggplot(., aes(x=total_cases)) +
geom_histogram(bins=8) +
theme_bw()
Error in eval(lhs, parent, parent) : object 'clean' not found
ui <- fluidPage(
# CODE BELOW: Add a titlePanel with an appropriate title
titlePanel("COVID-19"),
# REPLACE CODE BELOW: with theme = shinythemes::shinytheme("<your theme>")
theme = shinythemes::shinytheme("cerulean"),
# shinythemes::themeSelector(),
sidebarLayout(
sidebarPanel(
selectInput('country', 'Select Country', top_7countries$country, multiple = TRUE),
dateRangeInput("date", "Select a Date Range",
start = "2020-02-15",
end = "2020-08-15")
),
mainPanel(
tabsetPanel(
tabPanel('Plot', plotly::plotlyOutput('plot_top_7countries')),
tabPanel('Table', DT::DTOutput('table_top_7countries'))
)
)
)
)
server <- function(input, output, session){
# Function to plot trends in a name
plot_trends <- function(){
top_7countries %>%
filter(country == input$country, date >= "2020-02-15") %>%
ggplot(aes(x = date, y = total_cases)) +
geom_line()
}
output$plot_top_7countries <- plotly::renderPlotly({
validate(
need(input$country != "", "Select a country and date range to get the app working")
)
plot_trends()
})
output$table_top_7countries <- DT::renderDT({
top_7countries %>%
filter(country == input$country)
})
}
shinyApp(ui = ui, server = server)
Listening on http://127.0.0.1:6288
NA
# Group by country, summarize to calculate total cases, find the top 7
coronavirus <- read_csv("./data/coronavirus.csv")
Parsed with column specification:
cols(
date = col_date(format = ""),
province = col_logical(),
country = col_character(),
lat = col_double(),
long = col_double(),
type = col_character(),
cases = col_double()
)
45800 parsing failures.
row col expected actual file
37001 province 1/0/T/F/TRUE/FALSE Alberta './data/coronavirus.csv'
37002 province 1/0/T/F/TRUE/FALSE Alberta './data/coronavirus.csv'
37003 province 1/0/T/F/TRUE/FALSE Alberta './data/coronavirus.csv'
37004 province 1/0/T/F/TRUE/FALSE Alberta './data/coronavirus.csv'
37005 province 1/0/T/F/TRUE/FALSE Alberta './data/coronavirus.csv'
..... ........ .................. ....... ........................
See problems(...) for more details.
bycountry <- coronavirus %>%
mutate(date = as.Date(date))%>%
filter(type == "confirmed") %>%
group_by(country, date) %>%
summarise(total_cases = sum(cases)) %>%
arrange(-total_cases)
`summarise()` regrouping output by 'country' (override with `.groups` argument)
target = c("Brazil", "India", "Mexico", "Peru", "Russia", "South Africa", "US")
top_7countries = bycountry %>%
filter(country %in% target) %>%
filter(total_cases > 0)
top_7countries
ui <- fluidPage(
# CODE BELOW: Add a titlePanel with an appropriate title
titlePanel("COVID-19"),
# REPLACE CODE BELOW: with theme = shinythemes::shinytheme("<your theme>")
theme = shinythemes::shinytheme("superhero"),
sidebarLayout(
sidebarPanel(
selectInput('country', 'Select Country', top_7countries$country, multiple = TRUE),
dateRangeInput("date", "Select a Date Range",
start = "2020-02-15",
end = "2020-08-15")
),
mainPanel(
tabsetPanel(
tabPanel('Plot', plotly::plotlyOutput('plot_top_7countries')),
tabPanel('Table', DT::DTOutput('table_top_7countries'))
)
)
)
)
server <- function(input, output, session){
# Function to plot trends in a name
plot_trends <- function(){
top_7countries %>%
filter(country == input$country, date >= "2020-02-15") %>%
ggplot(aes(x = date, y = total_cases)) +
geom_line()
}
output$plot_top_7countries <- plotly::renderPlotly({
validate(
need(input$country != "", "Select a country and date range to get the app working")
)
plot_trends()
})
output$table_top_7countries <- DT::renderDT({
top_7countries %>%
filter(country == input$country)
})
}
shinyApp(ui = ui, server = server)
Listening on http://127.0.0.1:6288
NA
# Group by country, summarize to calculate total cases, find the top 7
owid_covid <- read_csv("./data/owid-covid-data.csv")
Parsed with column specification:
cols(
.default = col_double(),
iso_code = col_character(),
continent = col_character(),
location = col_character(),
date = col_date(format = ""),
new_tests = col_logical(),
total_tests = col_logical(),
total_tests_per_thousand = col_logical(),
new_tests_per_thousand = col_logical(),
new_tests_smoothed = col_logical(),
new_tests_smoothed_per_thousand = col_logical(),
tests_per_case = col_logical(),
positive_rate = col_logical(),
tests_units = col_logical()
)
See spec(...) for full column specifications.
108700 parsing failures.
row col expected actual file
1169 new_tests 1/0/T/F/TRUE/FALSE 2.0 './data/owid-covid-data.csv'
1169 total_tests 1/0/T/F/TRUE/FALSE 2.0 './data/owid-covid-data.csv'
1169 total_tests_per_thousand 1/0/T/F/TRUE/FALSE 0.0 './data/owid-covid-data.csv'
1169 new_tests_per_thousand 1/0/T/F/TRUE/FALSE 0.0 './data/owid-covid-data.csv'
1169 tests_units 1/0/T/F/TRUE/FALSE people tested './data/owid-covid-data.csv'
.... ........................ .................. ............. ............................
See problems(...) for more details.
owid_bycountry = owid_covid %>%
group_by(location, date) %>%
summarize(total_cases = sum(total_cases))
`summarise()` regrouping output by 'location' (override with `.groups` argument)
owid_bycountry
target = c("Brazil", "India", "Mexico", "Peru", "Russia", "South Africa", "United States")
top_7countries_OW = owid_bycountry %>%
group_by(location) %>%
filter(location %in% target) %>%
filter(total_cases > 0)
top_7countries_OW
ui <- fluidPage(
# CODE BELOW: Add a titlePanel with an appropriate title
titlePanel("COVID-19"),
# REPLACE CODE BELOW: with theme = shinythemes::shinytheme("<your theme>")
theme = shinythemes::shinytheme("cerulean"),
# shinythemes::themeSelector(),
sidebarLayout(
sidebarPanel(
selectInput('location', 'Select Country', top_7countries_OW$location),
# sliderInput("month", "Select Month:",
# min = 2, max = 8, value = 4,
# )
selectInput('month','Select Month', top_7countries_OW$month, multiple = TRUE)
),
mainPanel(
tabsetPanel(
tabPanel('Plot', plotly::plotlyOutput('plot_top_7countries_OW')),
tabPanel('Table', DT::DTOutput('table_top_7countries_OW'))
)
)
)
)
server <- function(input, output, session){
# Function to plot trends in a name
plot_trends <- function(){
top_7countries_OW %>%
filter(location == input$location) %>%
filter(month == input$month) %>%
ggplot(aes(x = date, y = total_cases)) +
geom_line()
}
output$plot_top_7countries_OW <- plotly::renderPlotly({
validate(
need(input$location != "", "Select a country to get the app working")
)
plot_trends()
})
output$table_top_7countries_OW <- DT::renderDT({
top_7countries_OW %>%
filter(location == input$location)%>%
filter(month == input$month) %>%
select(location, date, total_cases)
})
}
shinyApp(ui = ui, server = server)
Listening on http://127.0.0.1:6288
Error in : Problem with `filter()` input `..1`.
x Input `..1` must be of size 194 or 1, not size 0.
ℹ Input `..1` is `month == input$month`.
ℹ The error occured in group 1: location = "Italy". 124: <Anonymous>
NA
ui <- fluidPage(
# set theme
theme = shinythemes::shinytheme("cerulean"),
# shinythemes::themeSelector(),
## attempt to create multi pages
tags$head(
tags$style(HTML("
.navbar .navbar-header {float: right}
"))
),
navbarPage("WORLD", icon=icon('globe'),
title="COVID-19 Worldwide by the Numbers",
id="nav",
position="fixed-top",
collapsible=TRUE,
br(),
br(),
br(),
fluidRow(h1("World COVID-19 numbers by Month")),
fluidRow(
sidebarLayout(
sidebarPanel(
selectInput('location', 'Select Country', top_7countries_OW$location),
# sliderInput("month", "Select Month:",
# min = 2, max = 8, value = 4,
# )
selectInput('month','Select Month', top_7countries_OW$month, multiple = TRUE)
),
mainPanel(
tabsetPanel(
tabPanel('Plot', plotly::plotlyOutput('plot_top_7countries_OW')),
tabPanel('Table', DT::DTOutput('table_top_7countries_OW'))
)
)
)
)
), #end of first tabPanel
navbarMenu("EXPLORE", icon=icon('compass'),
br(),
br(),
br(),
fluidRow(h1("Renewable Energy Insights by Region")),
fluidRow(
sidebarLayout(
sidebarPanel(
selectInput('location', 'Select Country', top_7countries_OW$location),
sliderInput("month", "Select Month:",
min = 2, max = 8, value = 4,
),
# selectInput('month','Select Month', top_7countries_OW$month, multiple = TRUE)
# ),
mainPanel(
tabsetPanel(
tabPanel('Plot', plotly::plotlyOutput('plot_top_7countries_OW')),
tabPanel('Table', DT::DTOutput('table_top_7countries_OW'))
)
)
)
)
)
)
)
Error in buildTabset(tabs, "nav navbar-nav", NULL, id, selected) :
Tabs should all be unnamed arguments, but some are named: icon